#include "stdafx.h"
#include "MallManager.h"

MallManager::MallManager()
{
}
MallManager::~MallManager()
{

}

void MallManager::loadMallCSV()
{
	if(m_mapMallIngotCSV.size() != 0)
		return;
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "csv/mall_ingot.csv";
#ifndef _WIN32
	filename = "./csv/mall_ingot.csv";
#endif
	m_csvReader.Load(filename);

	for (Int32 i = 0; i < m_csvReader.GetRowCount(); i++)
	{
		Int32 id = m_csvReader.GetInt(i,"id");
		if(m_mapMallIngotCSV.find(id) != m_mapMallIngotCSV.end())
		{
			LOGGER_ERROR("m_mapMallCSV.find(id) != m_mapMallCSV.end(),id:%d",id);
			return;
		}

		m_mapMallIngotCSV[id].csv_id = id;
		MallCSVInfo& info = m_mapMallIngotCSV[id];

		info.type = m_csvReader.GetInt(i,"type");
		info.item_id = m_csvReader.GetInt(i,"item_id");
		info.price = m_csvReader.GetInt(i,"price");
	}

	//------------------------------------------
	if(m_mapMallMoneyCSV.size() != 0)
		return;
	path = singleton<GameServer>::instance().GetServerPath();
	filename = path + "csv/mall_money.csv";
#ifndef _WIN32
	filename = "./csv/mall_money.csv";
#endif
	m_csvReader.Load(filename);

	for (Int32 i = 0; i < m_csvReader.GetRowCount(); i++)
	{
		Int32 id = m_csvReader.GetInt(i,"id");
		if(m_mapMallMoneyCSV.find(id) != m_mapMallMoneyCSV.end())
		{
			LOGGER_ERROR("m_mapMallCSV.find(id) != m_mapMallCSV.end(),id:%d",id);
			return;
		}

		m_mapMallMoneyCSV[id].csv_id = id;
		MallCSVInfo& info = m_mapMallMoneyCSV[id];

		info.type = m_csvReader.GetInt(i,"type");
		info.item_id = m_csvReader.GetInt(i,"item_id");
		info.price = m_csvReader.GetInt(i,"price");
	}

	//------------------------------------------
	if(m_mapMallHonourCSV.size() != 0)
		return;
	path = singleton<GameServer>::instance().GetServerPath();
	filename = path + "csv/mall_honour.csv";
#ifndef _WIN32
	filename = "./csv/mall_honour.csv";
#endif
	m_csvReader.Load(filename);

	for (Int32 i = 0; i < m_csvReader.GetRowCount(); i++)
	{
		Int32 id = m_csvReader.GetInt(i,"id");
		if(m_mapMallHonourCSV.find(id) != m_mapMallHonourCSV.end())
		{
			LOGGER_ERROR("m_mapMallCSV.find(id) != m_mapMallCSV.end(),id:%d",id);
			return;
		}

		m_mapMallHonourCSV[id].csv_id = id;
		MallCSVInfo& info = m_mapMallHonourCSV[id];

		info.type = m_csvReader.GetInt(i,"type");
		info.item_id = m_csvReader.GetInt(i,"item_id");
		info.price = m_csvReader.GetInt(i,"price");
	}
}
MallCSVInfo* MallManager::GetMallCSVInfo(Int32 type,Int32 csv_id)
{
	if(type == MALL_TYPE_INGOT)
	{
		std::map<Int32,MallCSVInfo>::iterator iter = m_mapMallIngotCSV.find(csv_id);
		if (iter != m_mapMallIngotCSV.end())
			return &(iter->second);
	}
	if(type == MALL_TYPE_MONEY)
	{
		std::map<Int32,MallCSVInfo>::iterator iter = m_mapMallMoneyCSV.find(csv_id);
		if (iter != m_mapMallMoneyCSV.end())
			return &(iter->second);
	}
	if(type == MALL_TYPE_HONOUR)
	{
		std::map<Int32,MallCSVInfo>::iterator iter = m_mapMallHonourCSV.find(csv_id);
		if (iter != m_mapMallHonourCSV.end())
			return &(iter->second);
	}
	return NULL;
}